//
//  ST_ConversionInputViewController.swift
//  btvideo
//
//  Created by mac on 2020/10/12.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyUserDefaults

class ST_ConversionInputViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title_nav_btn.setTitleForAllStates("绑定\(pd_Name)")
        
        self.right_nav_btn.setTitleForAllStates("保存")
        right_nav_btn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        right_nav_btn.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xDFE3E9)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)
        right_nav_btn.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xFEC207)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.selected)
        right_nav_btn.cornerRadius = 15
        right_nav_btn.snp.remakeConstraints { (make) in
            make.right.equalTo(own_navigation_bar).offset(-15)
            make.centerY.equalTo(title_nav_btn)
            make.width.equalTo(75)
            make.height.equalTo(30)
        }
        right_nav_btn.isHidden = false
        
        view.addSubview(item0)
        view.addSubview(item1)
        view.addSubview(item2)
        
        
        item0.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
            make.top.equalTo(own_navigation_bar.snp.bottom).offset(0)
        }
        
        item1.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
            make.top.equalTo(item0.snp.bottom)
        }
        
        item2.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
            make.top.equalTo(item1.snp.bottom)
        }
        
        
//        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: OperationQueue.main) {[weak self] (notification) in
//
//
//            guard let str0 = self?.item0.textField.text,str0.count > 1,let str1 = self?.item1.textField.text,str1.count > 1,let str2 = self?.item2.textField.text,str2.count > 1 else{
//
//                self?.right_nav_btn.isSelected = false
//
//                return
//            }
//
//            self?.right_nav_btn.isSelected = true
//        }
        
    }
    
    
    var pd_Name = ""
    
    fileprivate lazy var item0:ST_ConversionInputItemView = {
       let control = ST_ConversionInputItemView()
        control.textField.delegate = self
        control.data = ST_ConversionInputItemViewStruct.init(title: "\(pd_Name)账号", placeHolder: "填写\(pd_Name)账号")
        return control
    }()
    
    
    fileprivate lazy var item1:ST_ConversionInputItemView = {
       let control = ST_ConversionInputItemView()
        control.textField.delegate = self
        control.data = ST_ConversionInputItemViewStruct.init(title: "姓名", placeHolder: "\(pd_Name)实名认证的姓名")
        return control
    }()
    
    fileprivate lazy var item2:ST_ConversionInputItemView = {
       let control = ST_ConversionInputItemView()
        control.textField.delegate = self
        control.data = ST_ConversionInputItemViewStruct.init(title: "联系方式", placeHolder: "填写您的联系方式")
        return control
    }()
    
    fileprivate lazy var textFieldSet:Set<UITextField> = {
        return [item0.textField,item1.textField,item2.textField]
    }()

    var append_closure:((String)->())?
    
    
    override func right_click() {
        
        if !right_nav_btn.isSelected{
            return
        }
        
        guard let str0 = self.item0.textField.text,str0.count > 1,let str1 = self.item1.textField.text,str1.count > 1,let str2 = self.item2.textField.text,str2.count > 1 else{

            return
        }
        
        self.view.endEditing(true)
        
        let param = ["account":str0,"name":str1,"phone":str2]
        
        JVrequest(PH.setMyIncomeAccountApi,parameters: param, success: { (json) in
            
            self.append_closure?(str0)
            
            self.goback_previous_controller()
            
        }) { (code, err) in
            
            self.toast(text: err)
        }
        
        
    }
    
}


extension ST_ConversionInputViewController:UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        
        
        
        guard var animal = textField.text else{
            return true
        }
        
        if let idxRange = Range.init(range, in: animal){
            
            animal.replaceSubrange(idxRange, with: string)
            
            let set = textFieldSet.subtracting([textField])
            
//            当前输入的控件 文字大于 1 &&   没有找到 其他两个控件少于2个字的
            
            let canNextStep = animal.count > 1 && set.contains(where: {( $0.text?.count ?? 0 < 2 )}) == false
            
            self.right_nav_btn.isSelected = canNextStep
            
        }
        return true
    }
    
    
}


//MARK: - Item View
class ST_ConversionInputItemView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(115)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.bottom.right.equalToSuperview()
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(1)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var line:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#333333")
        control.numberOfLines = 0
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    lazy var textField:UITextField = {
        let control = UITextField()
        control.text = ""
        control.tintColor = UIColor(hexString: "#333333")
        control.textColor = UIColor(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    var data:ST_ConversionInputItemViewStruct?{
        didSet{
            guard let d = data else { return }
            
            titleLabel.text = d.title
            textField.placeholder = d.placeHolder
        }
    }
    
}


struct ST_ConversionInputItemViewStruct {
    var title:String
    var placeHolder:String
}
